//
//  QXPhotoViewerCollectionCell.swift
//  QXWeibo
//
//  Created by QinXin on 15/7/3.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD
import FFAutoLayout

class QXPhotoViewerCollectionCell: UICollectionViewCell, UIScrollViewDelegate {
    
    var imageURL: NSURL? {
        didSet {
                        
            actIndicator.startAnimating()
            
            // 从缓冲池出来后，先要初始化
            self.resetScrollView()

//            // 异步加载图片，无大小
//            imageView.sd_setImag ,eWithURL(imageURL)
            
            imageView.sd_setImageWithURL(imageURL!, placeholderImage: nil) { (image, error, _, _) -> Void in
                
                if image != nil {
                    
                    var w: CGFloat
                    var h: CGFloat
                    var x: CGFloat
                    var y: CGFloat
                    
                    let winSize = UIScreen.mainScreen().bounds.size
                    let imgSize = image.size
                    
                    let k = winSize.width / winSize.height
                    let k1 = imgSize.width / imgSize.height
                    if k > k1 {
                        w = winSize.width
                        h = w / k1
                        x = 0
                        y = 0
                    } else {
                        w = winSize.width
                        h = w / k1
                        x = 0
                        y = (winSize.height - h) * 0.5
                    }

                    self.imageView.frame = CGRect(x: 0, y: 0, width: w, height: h)
                    self.scrollView.contentSize = CGSize(width: w, height: h)
                    self.scrollView.contentInset = UIEdgeInsets(top: y, left: x, bottom: 0, right: 0)
                    
                } else {
                    SVProgressHUD.showErrorWithStatus("加载失败！")
                }
                self.actIndicator.stopAnimating()
            }
            
        }
    }
    
    lazy var scrollView = UIScrollView()
    lazy var imageView = UIImageView()
    lazy private var actIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
    // 这时候尺寸已经确定
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(scrollView)
        scrollView.addSubview(imageView)
        addSubview(actIndicator)
        
        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2
        
        scrollView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))
        actIndicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
        
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func resetScrollView() {
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offsetX < 0 {
            offsetX = 0
        }
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
    }

}
